package com.freewater.library.catalogue.infrastructure.repository;

import com.freewater.library.catalogue.infrastructure.dao.BookDao;
import com.freewater.library.catalogue.infrastructure.po.BookPo;
import com.freewater.library.catalogue.model.Book;
import com.freewater.library.catalogue.model.BookRepository;
import lombok.RequiredArgsConstructor;

import java.util.Optional;

@RequiredArgsConstructor
public class BookRepositoryImpl implements BookRepository {

    private final BookDao bookDao;

    @Override
    public void save(Book book) {
        this.bookDao.save(new BookPo(book));
    }

    @Override
    public boolean exists(String isbn) {
        return this.bookDao.existsByIsbn(isbn);
    }

    @Override
    public void delete(Book book) {
        this.bookDao.delete(new BookPo(book));
    }

    @Override
    public Optional<Book> findById(String isbn) {
        Optional<BookPo> recordOpt = this.bookDao.findByIsbn(isbn);
        return recordOpt.map(BookPo::convert);
    }
}
